package com.tanhua.server.service;


import com.tanhua.domain.vo.QuestionVo;
import com.tanhua.domain.vo.ReportVo;
import com.tanhua.dubbo.api.SoulApi;

import com.tanhua.server.Interceptor.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SoulService {
    @Reference
    private SoulApi soulApi;


    //问卷列表
    public ResponseEntity<Object> showQuestions() {
        // Report report= soulApi.look(UserHolder.getUserId());

        List<QuestionVo> questionVoList =soulApi.showQuestions(UserHolder.getUserId());
        return ResponseEntity.ok(questionVoList);



    }
    //提交问卷答案
    public ResponseEntity<Object> submitQuestion(Map<String, List<Map<String, String>>> answers) {
        Long userId = UserHolder.getUserId();


        Map<String, Integer> map1= soulApi.queryScore(answers);
        Integer sum = map1.get("score");
        Integer state = map1.get("state");
        Integer conclusion=null;
        if(sum<21){
            conclusion=1;
        }else if(sum<40){
            conclusion=2;
        }else if(sum<56){
            conclusion=3;
        }else {
            conclusion=4;
        }



        ObjectId reportId =soulApi.saveReport(userId,sum,conclusion,state);
        String report=reportId.toString();
        return ResponseEntity.ok(report);
    }
    //查看结果
    public ResponseEntity<Object> queryResult(String id) {

        ReportVo reportVo= soulApi.queryResult(id);
        return ResponseEntity.ok(reportVo);

    }
}
